package com.hightrast.service.controller;


import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hightrast.service.api.baseEntity.R;
import com.hightrast.service.api.baseEntity.exception.ServiceException;
import com.hightrast.service.api.domain.bo.QueryShopifyOrders;
import com.hightrast.service.api.domain.bo.QueryShopifyProduct;
import com.hightrast.service.api.domain.bo.ShopifyOrdersResultEntity;
import com.hightrast.service.api.domain.bo.ShopifyProductResultEntity;
import com.hightrast.service.api.entity.RastOrders;
import com.hightrast.service.api.entity.RastProducts;
import com.hightrast.service.api.entity.RastUser;
import com.hightrast.service.component.MailClient;
import com.hightrast.service.component.ShopifyApiComponent;
import com.hightrast.service.converToClassMapper.ProductMapper;
import com.hightrast.service.service.IRastOrdersService;
import com.hightrast.service.service.IRastProductsService;
import com.hightrast.service.service.IRastUserService;
import com.hightrast.service.task.ScheduledTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 开放接口
 * </p>
 *
 * @author hx
 * @since 2025-01-11
 */
@RestController
@RequestMapping("/api/open")
@Slf4j
@Api(tags = "开放接口")
public class RastOpenController {

    @Resource
    ShopifyApiComponent shopifyApiComponent;
    @Resource
    private MailClient mailClient;
    @Resource
    IRastOrdersService ordersService;
    @Resource
    IRastProductsService productsService;
    @Resource
    IRastUserService userService;
    @Resource
    ScheduledTask schedulerTask;

    @ApiOperation("发送邮件测试")
    @GetMapping("/senMail")
    public R<Void> senMail(@RequestParam("mailAddress") String mailAddress){
        mailClient.sendMail(mailAddress,"测试","芜湖邮件来喽！");
        return R.success();
    }
    @ApiOperation("同步商品列表")
    @PostMapping("/testProducts")
    public R<List<RastProducts>> syncProducts(@RequestBody List<QueryShopifyProduct> products) {
        ShopifyProductResultEntity productResultEntity = shopifyApiComponent.syncProducts();
        if(null != productResultEntity && CollUtil.isNotEmpty(productResultEntity.getProducts())){
            products = productResultEntity.getProducts();
        }
        List<RastProducts> rastProducts = products.stream()
                .map(ProductMapper.INSTANCE::queryShopifyToProduct)
                .collect(Collectors.toList());
        productsService.addProduct(rastProducts);
        return R.data(rastProducts);
    }

    @ApiOperation("同步订单列表")
    @PostMapping("/testAddOrder")
    public R<List<RastOrders>> testAddOrder(@RequestBody List<QueryShopifyOrders> orderList) {
        ShopifyOrdersResultEntity ordersResultEntity = shopifyApiComponent.syncOrders();
        if(null != ordersResultEntity && CollUtil.isNotEmpty(ordersResultEntity.getOrders())){
            orderList = ordersResultEntity.getOrders();
        }
        List<RastOrders> rastOrders = ordersService.addQo(orderList);
        ordersService.addOrders(rastOrders);
        return R.data(rastOrders);
    }

    @ApiOperation("发送邮箱验证码")
    @GetMapping("/senMailVerifyCode")
    public R<Void> senMailVerifyCode(@RequestParam("mailAddress") String mailAddress, @RequestParam("way") String way, @RequestParam("page") String page){
        mailClient.senMailVerifyCode(mailAddress, way, page);
        return R.success();
    }

    @ApiOperation("核实验证码是否正确")
    @GetMapping("/checkingVerifyCode")
    public R<Void> checkingVerifyCode(@RequestParam String email, @RequestParam String code, @RequestParam String way) {
        boolean check = mailClient.checkingVerifyCode(email, code, way);
        if (check) {
            return R.success();
        } else {
            // 验证码错误，请重新输入
            return R.fail("Verification code error, please re-enter!!!");
        }
    }

    @ApiOperation("修改密码")
    @GetMapping("/editPassword")
    public R<Void> editPassword(@RequestParam @Validated String email, @RequestParam @Validated String password) {
        RastUser user = userService.getOne(new LambdaQueryWrapper<RastUser>().eq(RastUser::getEmail, email));
        if (ObjectUtil.isEmpty(user)) {
            throw new ServiceException("This email is not registered");
        }
        // 加密密码
        user.setPassword(BCrypt.hashpw(password));
        userService.updateById(user);
        return R.success();
    }

    @ApiOperation("发送设计师品牌月度销售数据")
    @GetMapping("/salesDataPushTask")
    public R<Void> salesDataPushTask() {
        try {
            schedulerTask.salesDataPushTask();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return R.success();
    }


}
